from django.shortcuts import render
from rest_framework.generics import GenericAPIView, ListCreateAPIView
from rest_framework.views import APIView
from rest_framework.response import Response
from shopmall import settings
from goods.models import *
from goods.ser import *

import os

from users.utils import MyPagination


class UploadView(APIView):
    def post(self, request):
        """上传图片"""
        files = request.FILES.get('file')
        file_dir = settings.STATICFILES_DIRS[0]
        file_name = file_dir + files.name
        print('图片路径前缀》》', file_dir)
        print('图片路径》》', file_name)

        with open(file_name, 'wb') as f:
            f.write(files.file.read())

        return Response({
            'code': 200,
            'msg': '上传成功',
            'file_path': file_name,
            'file_name': 'http://127.0.0.1:8000/static/images/'+ files.name
        })


class AddBrand(APIView):
    def post(self, request):
        brand_name = request.data.get('brand_name')
        first_letter = request.data.get('first_letter')
        logo = request.data.get('logo')
        print('logo>>',logo)
        try:
            Brand.objects.create(brand_name=brand_name, first_letter=first_letter, logo_addr=logo)
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '添加失败',
            })
        return Response({
            'code': 200,
            'msg': '添加成功',
        })


class GetBrand(ListCreateAPIView):
    pagination_class = MyPagination
    queryset = Brand.objects.all()
    serializer_class = BrandSer


class BrandShow(APIView):
    def get(self, request, id):
        try:
            brand = Brand.objects.get(id=id)
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '获取失败',
            })
        ser = BrandSer(brand)
        return Response({
            'code': 200,
            'msg': '获取成功',
            'brand': ser.data
        })

    def put(self, request, id):
        brand_name = request.data.get('brand_name')
        first_letter = request.data.get('first_letter')
        logo = request.data.get('logo')
        print('要修改成的logo>>', logo)
        try:
            brand = Brand.objects.get(id=id)
            print('数据库现在的图片》',brand.logo_addr)
            brand.brand_name = brand_name
            brand.first_letter = first_letter
            brand.logo_addr = logo
            brand.save()
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '添加失败',
            })
        return Response({
            'code': 200,
            'msg': '添加成功',
        })


class SkuView(APIView):
    def get(self, request):
        try:
            # 获取查询集，所有的品牌
            skus = SKU.objects.all()
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '获取失败',
            })
        ser = SKUSer(skus, many=True)
        return Response({
            'code': 200,
            'msg': '获取成功',
            'skus': ser.data
        })


class GetSkuView(APIView):
    def get(self, request, id):
        try:
            # 获取查询集，所有的品牌
            skus = SKU.objects.get(id=id)
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '获取失败',
            })
        ser = SKUSer(skus)
        return Response({
            'code': 200,
            'msg': '获取成功',
            'skus': ser.data
        })


class UpdateSku(APIView):
    def put(self, request, id):
        name = request.data.get('name')
        spu = request.data.get('spu')
        describe = request.data.get('describe')
        cate = request.data.get('cate')
        price = request.data.get('price')
        stock = request.data.get('stock')
        is_selling = request.data.get('is_selling')
        try:
            skus = SKU.objects.get(id=id)
            skus.name = name
            skus.describe = describe
            skus.price = price
            skus.stock = stock
            skus.is_selling = is_selling
            skus.save()
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '修改失败',
            })
        return Response({
            'code': 200,
            'msg': '修改成功'
        })


class AddSku(APIView):
    def post(self, request):
        name = request.data.get('name')
        spu = request.data.get('spu')
        describe = request.data.get('describe')
        cate = request.data.get('cate')
        price = request.data.get('price')
        stock = request.data.get('stock')
        is_selling = request.data.get('is_selling')
        if is_selling=='true':
            is_selling = True
        else:
            is_selling = False
        try:
            SKU.objects.create(
                name = name,
                spu_id=1,
                cate_id=1,
                describe = describe,
                price = price,
                stock = stock,
                is_selling =is_selling
            )
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '添加失败',
            })
        return Response({
            'code': 201,
            'msg': '添加成功'
        })


class DelSku(APIView):
    def delete(self, request, id):
        try:
            skus = SKU.objects.get(id=id)
            skus.delete()
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '删除失败',
            })
        return Response({
            'code': 200,
            'msg': '删除成功'
        })


class SpuView(APIView):
    def get(self, request):
        try:
            spus = SPU.objects.all()
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '获取失败',
            })
        ser = SPUSer(spus, many=True)
        return Response({
            'code': 200,
            'msg': '获取成功',
            'spus': ser.data
        })

    def post(self, request):
        name = request.data.get('name')
        brand_id = request.data.get('brand')
        category1_id = request.data.get('category1')
        category2_id = request.data.get('category2')
        category3_id = request.data.get('category3')
        desc_detail = request.data.get('desc_detail')
        desc_pack = request.data.get('name')
        desc_service = request.data.get('name')
        brand = Brand.objects.get(id=brand_id)
        cate1 = Category.objects.get(id=category1_id)
        cate2 = Category.objects.get(id=1)
        cate3 = Category.objects.get(id=1)
        try:
            SPU.objects.create(
                name=name,
                brand=brand,
                category1=cate1,
                category2=cate1,
                category3=cate1,
                desc_detail=desc_detail,
                desc_pack=desc_pack,
                desc_service=desc_service
            )
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '添加失败',
            })
        return Response({
            'code': 200,
            'msg': '添加成功'
        })


class GetSpuView(APIView):
    def get(self, request, id):
        try:
            spus = SPU.objects.get(id=id)
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '获取失败',
            })
        ser = SPUSer(spus)
        return Response({
            'code': 200,
            'msg': '获取成功',
            'spus': ser.data
        })


class DelSpuView(APIView):
    def delete(self, request, id):
        try:
            SPU.objects.get(id=id).delete()
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '删除失败',
            })
        return Response({
            'code': 200,
            'msg': '删除成功'
        })


class UpSpuView(APIView):
    def put(self, request, id):
        name = request.data.get('name')
        brand_id = request.data.get('brand_id')
        category1_id = request.data.get('category1_id')
        category2_id = request.data.get('category2_id')
        category3_id = request.data.get('category3_id')
        desc_detail = request.data.get('desc_detail')
        desc_pack = request.data.get('name')
        desc_service = request.data.get('name')
        try:
            spu = SPU.objects.get(id=id)
            spu.name=name
            spu.brand=brand_id
            spu.category1=category1_id
            spu.category2=category2_id
            spu.category3=category3_id
            spu.desc_detail=desc_detail
            spu.desc_pack=desc_pack
            spu.desc_service=desc_service
            spu.save()
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '修改失败',
            })
        return Response({
            'code': 200,
            'msg': '修改成功'
        })


class BrandAll(APIView):
    def get(self, request):
        try:
            brand = Brand.objects.all()
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '获取失败',
            })
        ser = BrandSer(brand, many=True)
        return Response({
            'code': 200,
            'msg': '获取成功',
            'brand': ser.data
        })


from category_group.models import *
from category_group.ser import *


class CateTwo(APIView):
    def get(self, reqeuest, id):
        try:
            cate = Category.objects.get(id=id)
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '获取失败',
            })
        ser = CategorySer(cate)
        return Response({
            'code': 200,
            'msg': '获取成功',
            'cate': ser.data
        })


from rest_framework.viewsets import ModelViewSet


class SpecView(ModelViewSet):
    queryset = SPUSpecification.objects.all()
    serializer_class = SpecSer
    pagination_class = MyPagination

    def update(self, request, *args, **kwargs):
        """重写修改方法"""

        partial = kwargs.pop('partial', False)
        instance = self.get_object()
        serializer = self.get_serializer(instance, data = request.data, partial=partial)
        serializer.is_valid(raise_exception = True)
        self.perform_update(serializer)
        return Response(serializer.data)


class SpecAll(APIView):
    def get(self, request):
        try:
            spec = SPUSpecification.objects.all()
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '获取规格失败'
            })
        ser = SpecSer(spec, many=True)
        return Response({
            'code': 200,
            'msg': '获取规格成功',
            'spec': ser.data
        })


class OptionView(APIView):
    def get(self, request):
        try:
            option = SpecificationOption.objects.all()
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '获取规格选项失败'
            })
        ser = OptionSer(option, many=True)
        return Response({
            'code': 200,
            'msg': '获取规格选项成功',
            'option': ser.data
        })

    def post(self, request):
        name = request.data.get('name')
        specs_id = request.data.get('specs_id')
        print('aaaa>>', specs_id)
        print('bbbb>>', name)
        try:
            spu_spec_id = SPUSpecification.objects.get(id=specs_id)
            SpecificationOption.objects.create(value=name, spec=spu_spec_id)
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '添加规格选项失败'
            })
        return Response({
            'code': 200,
            'msg': '添加规格选项成功',
        })


class SpecOptionView(APIView):
    def get(self, request, id):
        try:
            option = SpecificationOption.objects.get(id=id)
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '获取规格选项失败'
            })
        ser = OptionSer(option)
        return Response({
            'code': 200,
            'msg': '获取规格选项成功',
            'option': ser.data
        })

    def put(self, request, id):
        name = request.data.get('name')
        spec_id = request.data.get('spec_id')
        try:
            SpecificationOption.objects.filter(id=id).update(spec=spec_id, value=name)
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '修改规格选项失败'
            })
        return Response({
            'code': 200,
            'msg': '修改规格选项成功',
        })

    def delete(self, request, id):
        try:
            SpecificationOption.objects.get(id=id).delete()
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '删除规格选项失败'
            })
        return Response({
            'code': 200,
            'msg': '删除规格选项成功',
        })


class Pic(APIView):
    def post(self, request):
        image = request.data.get('image')
        sku = request.data.get('sku')
        print(image)
        print(sku)
        try:
            sku_id = SKU.objects.get(id=sku)
            SKUImage.objects.create(sku=sku_id, image=image)
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '添加商品图片失败'
            })
        return Response({
            'code': 200,
            'msg': '添加商品图片成功',
        })
    def get(self, request):
        try:
            pic = SKUImage.objects.all()
        except Exception as e:
            print(e)
            return Response({
                'code': 400,
                'msg': '获取商品图片失败'
            })
        ser = PicSer(pic, many=True)
        return Response({
            'code': 200,
            'msg': '获取商品图片成功',
            'pic':ser.data
        })

































































































































